from fastapi import APIRouter
import logging
import time

userRouter = APIRouter()

_LOGGER = logging.getLogger(__name__)


@userRouter.get("/userInfo")
async def getUserInfo():
    from lib.conftest import (
        test_cache_path as _test_cache_path,
        test_cloud_server as _test_cloud_server,
        test_oauth2_redirect_url as _test_oauth2_redirect_url,
        test_uuid as _test_uuid,
        test_domain_cloud_cache as _test_domain_cloud_cache,
        test_name_oauth2_info as _test_name_oauth2_info,
        test_name_uuid as _test_name_uuid
    )
    test_cache_path = _test_cache_path()
    test_cloud_server = _test_cloud_server()
    test_oauth2_redirect_url = _test_oauth2_redirect_url()
    test_uuid = _test_uuid()
    test_domain_cloud_cache = _test_domain_cloud_cache()
    test_name_oauth2_info = _test_name_oauth2_info()
    test_name_uuid = _test_name_uuid()

    from lib.miot.const import OAUTH2_CLIENT_ID
    from lib.miot.miot_cloud import MIoTOauthClient
    from lib.miot.miot_storage import MIoTStorage
    print("test_cache_path")
    print(test_cache_path)
    miot_storage = MIoTStorage(test_cache_path)
    local_uuid = await miot_storage.load_async(
        domain=test_domain_cloud_cache, name=test_name_uuid, type_=str)
    uuid = str(local_uuid or test_uuid)
    _LOGGER.info('uuid: %s', uuid)
    miot_oauth = MIoTOauthClient(
        client_id=OAUTH2_CLIENT_ID,
        redirect_url=test_oauth2_redirect_url,
        cloud_server=test_cloud_server,
        uuid=uuid)

    oauth_info = None
    load_info = await miot_storage.load_async(
        domain=test_domain_cloud_cache, name=test_name_oauth2_info, type_=dict)
    if (
            isinstance(load_info, dict)
            and 'access_token' in load_info
            and 'expires_ts' in load_info
            and load_info['expires_ts'] > int(time.time())
    ):
        _LOGGER.info('load oauth info, %s', load_info)
        oauth_info = load_info
    if oauth_info is None:
        return {
            'data': None
        }

    return {
        'data': oauth_info
    }


@userRouter.get("loginUrl")
async def loginUrl():
    from lib.conftest import (
        test_cache_path as _test_cache_path,
        test_cloud_server as _test_cloud_server,
        test_oauth2_redirect_url as _test_oauth2_redirect_url,
        test_uuid as _test_uuid,
        test_domain_cloud_cache as _test_domain_cloud_cache,
        test_name_oauth2_info as _test_name_oauth2_info,
        test_name_uuid as _test_name_uuid
    )
    test_cache_path = _test_cache_path()
    test_cloud_server = _test_cloud_server()
    test_oauth2_redirect_url = _test_oauth2_redirect_url()
    test_uuid = _test_uuid()
    test_domain_cloud_cache = _test_domain_cloud_cache()
    test_name_uuid = _test_name_uuid()

    from lib.miot.const import OAUTH2_CLIENT_ID
    from lib.miot.miot_cloud import MIoTOauthClient
    from lib.miot.miot_storage import MIoTStorage
    print("test_cache_path")
    print(test_cache_path)
    miot_storage = MIoTStorage(test_cache_path)
    local_uuid = await miot_storage.load_async(
        domain=test_domain_cloud_cache, name=test_name_uuid, type_=str)
    uuid = str(local_uuid or test_uuid)
    _LOGGER.info('uuid: %s', uuid)
    miot_oauth = MIoTOauthClient(
        client_id=OAUTH2_CLIENT_ID,
        redirect_url=test_oauth2_redirect_url,
        cloud_server=test_cloud_server,
        uuid=uuid)

    auth_url: str = miot_oauth.gen_auth_url()
    assert isinstance(auth_url, str)
    _LOGGER.info('auth url: %s', auth_url)
    return {
        "data": auth_url
    }
